Repository navigation
feat(libkernel): implement the named ksem semaphores - #9293
DavidSamir wants to merge 13 commits into
Conversation
…sed and replaced ids
Progress report (0c6a5bd → b208fd6)📈 System libraries: 42.19% (+0.09%, +10 implemented, -10 removed functions) ✅ 10 implemented
🗑️ 10 removed
|
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Exports with a name or library/NID already present in
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
…elative name An absolute host temp path is not mapped by the Linux guest resolver. Found by xXPBSXx on boykopovar#9097.
The PS5 3.20 export lists have ksem_* in libkernel as well as libkernel_sys, and on Windows a libkernel import never reaches libkernel_sys, so titles importing them from libkernel did not find them. Following pipe1os's review on boykopovar#9284, also: ENOSPC from ksem_init when no descriptor is left, the SEM_MAX limit of 30 with ENOSPC, and dup/dup2 from a ksem id throwing as for a kqueue.
… libkernel too Following the move of the unnamed calls: ksem_open now allocates the descriptor and writes the id before checking the name, as ksem_create does, and the 30-semaphore limit counts semaphore objects, with a linked name counting until it is unlinked (ENFILE for a new name).
|
Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head
Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine. |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — changes requested
Changes are required before merge: handle an empty semaphore name before indexing its first character.
What I ran
Reviewed head b208fd6b210d26e58b969d2341404b4cf085f5b0. I did not run tests or build commands. I read the prepared description, commits, diff, manifest, and all changed file paths; the file listing showed all nine changed files included. The review bundle did not provide files.json.
Results
| Check | Result | Note |
|---|---|---|
| Code review | failed | Empty name can trigger undefined behavior |
| Build and tests | not run | No commands run |
Findings
Blocking
core/libs/prx/libkernel/Semaphore/src/Ksem.cpp:211: An empty string is a valid non-nullname, but after constructing an emptypath, this line readspath[0]. That is out-of-bounds access and can cause undefined behavior instead of returningEINVAL. Checkpath.empty()before indexing.
Non-blocking
None.
Not verified
Build and test results were not independently verified. I reviewed all changed paths listed by the reader; the listing showed no excluded changed files.
To get this to a good state
Check for an empty path before reading path[0], then rerun the build and ksem tests on the updated head.
|
I am closing this for now. The review left three open points: the null name contract for ksem_open against the cited FreeBSD behaviour, replacing the duplicate void declarations with typed ones, and a merge with main. There has been no push since 21 Oct. Feel free to reopen or push a merged branch at any time and we will look at it again. |
What
Builds on #9284 and adds the named
ksem_*exports tolibkernel/Semaphore/src/Ksem.cpp, following FreeBSD 11sys/kern/uipc_sem.c. Until #9284 merges, this PR's diff also shows #9284's commits.Moved to libkernel so libkernel imports reach it on Windows, as #9284 now is. The PS5 3.20 lists export
ksem_*from libkernel too, and a Windows libkernel import never searches libkernel_sys. The libkernel_sys stubs forksem_open,ksem_closeandksem_unlinkare removed.ksem_open(semid_t* idp, const char* name, int oflag, mode_t mode, u_int value)ksem_close(semid_t)EINVALon an unnamed id, which needsksem_destroyksem_unlink(const char* name)ENOENTif it is absent; ids already open keep working, and a laterO_CREATopen makes a new semaphoreksem_open's rules, in the order ofsys_ksem_openandksem_create:EINVALforoflagbits other thanO_CREAT | O_EXCL, then forvalue > SEM_VALUE_MAX.EMFILEorENFILEwhen none is left), and the id is written to*idp(EFAULTfor a NULLidp) before the name is checked, as FreeBSD's early copyout does.EFAULTfor a NULL name,ENAMETOOLONGfor 1024 bytes or more, andEINVALfor a name that doesn't start with/.ENOENTfor a missing name withoutO_CREAT, andEEXISTfor an existing name withO_CREAT | O_EXCL.ENFILEwhen a new name would exceed the 30 semaphores (SEM_MAX).Each successful open returns a new descriptor id that shares the semaphore.
The limit counts semaphore objects, as FreeBSD's
nsemsdoes. Two opens of one name are one semaphore, and a name still linked counts untilksem_unlink, even with no open id. Unnamedksem_initgivesENOSPCpast the limit, as in #9284. (Points from @pipe1os's review on #9284.)ksem_destroynow givesEINVALon a named id, which needsksem_close, as FreeBSD'sKS_ANONYMOUScheck does. A debt line inlibkernel.mdrecords that named semaphores are kept per process, which fits a single guest process:modeis accepted, and the owner and permission checks are not modelled.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved.
guest_kernel_sys_ksemgainsNamed():ksem_destroyon a named id andksem_closeon an unnamed id giveEINVAL;ksem_unlinkthe name is gone (ENOENT), but the open ids still work;ksem_closeinvalidates its id, and the other id is unaffected;O_CREATopen after the unlink starts from its own value.NamedLimit():EFAULTwith the id already written but dead;ksem_initgivesENOSPCand a new name givesENFILE;ksem_unlink, there's room again.ksem_open_nid_postfix not implemented. On this branch it passes, 5 of 5 runs.tools/check_conventions.pypasses, including the duplicate-export rule (all tenksem_*are now exported by libkernel only).tools/pr_overlap.pycrashes on this machine's partial clone, so it was replaced by a check against an index of every open PR's diff plusgit merge-tree. This branch merges cleanly withmainand with feat(libkernel_sys): identify the initial pthread #9160, feat(libkernel_sys): implement uuidgen #9195, feat(libkernel_sys): implement wait3 and wait4 #9162, feat(libkernel_sys): implement statfs symlinkat and futimesat #9161, feat(libkernel): implement four pthread_attr forms #9156, feat(libkernel): implement getlogin #9171, feat(libkernel): implement pselect over select #9273, feat(libkernel): implement tcgetsid #9242, feat(libkernel_sys): implement pwrite through the shared POSIX provider #9019, feat(libkernel_sys): implement fstat #9008, feat(libkernel_sys): implement sceKernelFlock over flock #9097, feat(libkernel_sys): implement the scePthread scope, atfork and setconcurrency forms #9141, feat(libkernel_sys): implement chown, lchown, fchownat and linkat #9103, feat(libkernel_sys): implement the scePthreadBarrier, cpuclockid, concurrency and msync forms #8987 and feat(libkernel): report open, read and close notes on watched files #7121.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)